$(function(){
	checkLog();
	loadGuide();
	var pageSize = 100;
	var page = 0;
	var bookid = getSearchValue("bookid","pageparam_detail");
	var bookname = getSearchValue("bookname","pageparam_detail");
	var publishbookid = getSearchValue("publishbookid","pageparam_detail") || "";
	var getMore = $("#booklist-more");
	if(!bookid){
		window.history.go(-1);
	};
	createBarBread([{title:"首页",url:indexUrl},{title:"我的小说",url:"mybook-list.html"},{title:bookname}]);
	setTitle(bookname);
	creatChapterList();
	function creatChapterList(){
		baseLoad();
		$.ajax({
			url:APIURL + "/Novel/getNovelChapterList.php",
			data:"userId=" + userId() + "&bookId=" + bookid + "&page="+page+"&size="+pageSize,
			success:function(data){
				hideBaseLoad();
				data = $.parseJSON(data);
		        if(data.status != "FAILED"){
					page++;
					var d = data.value;
					var len = d.length;
					var str = "";
					if(pageSize == len){
						getMore.show();
					}else{
						getMore.hide();
					}
					for(var i = 0; i < len; i++){
						str += '<li><a href="javascript:;" data-id="'+d[i].id+'" data-publishid="'+d[i].publishId+'" data-orderid="'+d[i].orderId+'" data-name="'+d[i].title+'">'+d[i].title+'</a></li>';
					}
					$("#chapter").append(str);                        
		        }else{
		          layer.open({
		              content: data.value,
		              time: 1
		          }); 
		        }

			},
			error:function(err){
				hideBaseLoad();
		        layer.open({
		            content: err.value || err_txt,
		            time: 2
		        });
			}
		});		
	}

	$("#chapter").on("click","a",function(){
		localStorage.setItem("pageparam_edit",'bookid='+bookid+'&chapterid='+$(this).attr("data-id")+'&orderid='+$(this).attr("data-orderid")+'&publishid='+$(this).attr("data-publishid")+'&bookname='+bookname+'&chaptername='+encodeURIComponent($(this).attr("data-name"))+'&publishbookid='+publishbookid);
		window.location.href = "mybook-chapter-edit.html";
	})

	$(".control-btns a").on("click",function(){
		var index = $(this).index();
		var param = 'bookid=' + bookid + '&bookname=' + bookname + '&orderid=0' + '&publishbookid=' + publishbookid;
		if(index == 0){
			localStorage.setItem("pageparam_edit",param);
			window.location.href = "mybook-chapter-edit.html";
		}else if(index == 1){
			localStorage.setItem("pageparam_draft",param);
			window.location.href = "mybook-chapter-draft.html";
		}else if(index == 2){	
			if(!publishbookid || publishbookid == "0"){
				layer.open({
					title:"",
					content:"此书还没有发布到书城，只有在客户端APP才可以发布书。",
					btn:["知道了"]
				})
			}else{
				window.open("http://www.cdyt8.com/Web/list.php?id=" + publishbookid);
			}
		}
	});

	getMore.on("click",function(){
		creatChapterList();
	})

})